package hao.itheima.controller;

import hao.itheima.entity.PageBean;
import hao.itheima.entity.Result;
import hao.itheima.entity.Student;
import hao.itheima.service.StudentService;
import hao.itheima.service.impl.EmpServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {


    @Autowired
    private StudentService studentService;
    @Autowired
    private EmpServiceImpl empServiceImpl;

    /**
     * 分页查询
     *
     * @param name
     * @param degree
     * @param clazzId
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result page(String name,
                       Integer degree,
                       Integer clazzId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        //1、日志输出
        log.info("参数列表,{},{},{},{}", name, degree, clazzId, page);
        //2、直接调用service层进行数据查询
        PageBean pageBean = studentService.page(name, degree, clazzId, page, pageSize);
        return Result.success(pageBean);
    }

    /**
     * 删除学员
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}") // 注意这是路径参数, 不是普通参数
    public Result delete(@PathVariable List<Integer> ids) {
        //1、输出展示参数
        log.info("路径参数,数组形式,{}", ids);
        studentService.delete(ids);
        return Result.success();
    }

    /**
     * 新增学员
     *
     * @param student
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Student student) {
        //1、调用studentService进行插入数据
        studentService.save(student);
        return Result.success();
    }

    /**
     * 根据学员id查询学员信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        //1、输出展示,路径参数
        log.info("查询回显,{}", id);

        //2、调用逻辑处理层
        Student student = studentService.getById(id);

        return Result.success(student);
    }

    /**
     * 修改学员信息
     *
     * @param student
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Student student) {
        //1.查看前端传过来的数据
        log.info("修改数据,{}", student);
        //2.调用service层对数据进行处理
        studentService.update(student);
        //3.返回结果
        return Result.success();
    }

    @PutMapping("/violation/{id}/{score}")
    public Result violationScore(@PathVariable Integer id, @PathVariable Integer score) {
        //1.输出参数, 展示
        log.info("id:{},score:{}", id, score);
        //2、调用Service进行数据逻辑处理
        studentService.violationScore(id, score);
        return Result.success();
    }

}
